package _1_PIO.excel.样式;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 设置单元格的对齐方式
 */
public class _1_AlignmentOptions {
    @Test
    public void _2003(){}


    @Test
    public void _2007() throws IOException {

        Workbook workbook = new XSSFWorkbook();

        Sheet sheet = workbook.createSheet("new Sheet");

        Row row = sheet.createRow(2);
        row.setHeightInPoints(30);

        createCell(workbook, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
        createCell(workbook, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);
        createCell(workbook, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
        createCell(workbook, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);
        createCell(workbook, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);
        createCell(workbook, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
        createCell(workbook, row,  6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
        createCell(workbook, row,  7, HorizontalAlignment.CENTER, VerticalAlignment.CENTER);

        try(OutputStream outputStream = new FileOutputStream("D://workbook.xlsx")){
            workbook.write(outputStream);
        }
    }

    public static void createCell(Workbook workbook, Row row, int column, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment){
        Cell cell = row.createCell(column);
        cell.setCellValue("Align It");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(horizontalAlignment);
        cellStyle.setVerticalAlignment(verticalAlignment);
        cell.setCellStyle(cellStyle);
    }

}
